package com.yx.doodleview.util;

import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Looper;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.muddzdev.styleabletoastlibrary.StyleableToast;

import com.yx.doodleview.manager.BaseApplication;


public class UIUtils {


    public static Context getContext() {
        return BaseApplication.getAppContext();
    }

    /**
     * dip转换px
     */
    public static int dip2px(int dip) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    /**
     * px转换dip
     */
    public static int px2dip(int px) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    public static int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }


    /**
     * 判断当前线程是否是UI线程.
     *
     * @return
     */
    public static boolean isUIThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    public static View inflate(int resId) {
        return LayoutInflater.from(getContext()).inflate(resId, null);
    }

    /**
     * 获取屏幕宽度
     *
     * @return
     */
    public static int getScreenWidth() {
        return getContext().getResources().getDisplayMetrics().widthPixels;
    }

    /**
     * 获取屏幕高度
     *
     * @return
     */
    public static int getScreenHeight() {
        return getContext().getResources().getDisplayMetrics().heightPixels;
    }


    /**
     * 获取控件宽度
     *
     * @param view
     * @return
     */
    public static int getViewWidth(View view) {
        int width = 0;
        if (view != null) {
            width = view.getWidth();
            if (width <= 0) {
                measureView(view);
                width = view.getMeasuredWidth();
            }
        }
        return width;
    }

    /**
     * 获取控件高度
     *
     * @param view
     * @return
     */
    public static int getViewHeight(View view) {
        int height = 0;
        if (view != null) {
            height = view.getHeight();
            if (height <= 0) {
                measureView(view);
                height = view.getMeasuredHeight();
            }
        }
        return height;
    }

    /**
     * 更改控件高度
     *
     * @param view
     * @param height
     * @return
     */
    public static boolean setViewHeight(View view, int height) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params != null) {
            params.height = height;
            view.setLayoutParams(params);
            return true;
        }
        return false;
    }

    /**
     * 更改控件宽度
     *
     * @param view
     * @param width
     * @return
     */
    public static boolean setViewWidth(View view, int width) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (params != null) {
            params.width = width;
            view.setLayoutParams(params);
            return true;
        }
        return false;
    }


    public static int getScaleHeight(int originalWidth, int originalHeight, int scaleWidth) {
        return originalHeight * scaleWidth / originalWidth;
    }


    public static void measureView(View v) {
        if (v == null) {
            return;
        }
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        v.measure(w, h);
    }


    /**
     * 获取资源
     */
    public static Resources getResources() {
        return getContext().getResources();
    }

    /**
     * 获取文字
     */
    public static String getString(int resId) {
        return getResources().getString(resId);
    }

    /**
     * 获取文字数组
     */
    public static String[] getStringArray(int resId) {
        return getResources().getStringArray(resId);
    }

    /**
     * 获取dimen
     */
    public static int getDimens(int resId) {
        return getResources().getDimensionPixelSize(resId);
    }

    /**
     * 获取drawable
     */
    public static Drawable getDrawable(int resId) {
        return getResources().getDrawable(resId);
    }

    /**
     * 获取颜色
     */
    public static int getColor(int resId) {
        return getResources().getColor(resId);
    }

    /**
     * 获取颜色选择器
     */
    public static ColorStateList getColorStateList(int resId) {
        return getResources().getColorStateList(resId);
    }


//    private static Toast toast;
    private static StyleableToast toast;


    public static void showToast(String str) {
        if (TextUtils.isEmpty(str)) {
            str = "";
        }
        if (toast == null) {
            toast = new StyleableToast(UIUtils.getContext());
            toast.setBackgroundColor(Color.parseColor("#5B5E63"));
            toast.setTextColor(Color.WHITE);
            toast.setMaxAlpha();
        }

        if (str.length() < 15) {
            toast.setDuration(Toast.LENGTH_SHORT);
        } else {
            toast.setDuration(Toast.LENGTH_LONG);
        }
        toast.setToastMsg(str);
        toast.show();

    }



    /**
     * 呼出软键盘
     */
    public static void openKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

    /**
     * 关闭软键盘
     *
     * @param mEditText 输入框
     * @param mContext  上下文
     */
    public static void closeKeybord(EditText mEditText, Context mContext) {
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (mEditText.isFocusable()) {
            imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
        }

    }



    /**
     * 启动主应用程序
     */
    public static void startCurrentApp() {
        try {
            Intent intent = getContext().getPackageManager().getLaunchIntentForPackage(getContext().getPackageName());
            getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}


